/*
 * @Author: your name
 * @Date: 2020-09-14 11:20:02
 * @LastEditTime: 2020-09-14 14:07:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vue/Watcher.js
 */
class Watcher {
    constructor(vm, key, cb) {
        this.vm = vm
        //data中的属性名称
        this.key = key 
        //回调函数负责更新视图
        this.cb = cb
        
        //把Watcher对象记录到Deep类的静态属性target
        Dep.target = this
        //触发get方法，在get方法中调用addSub
        this.oldValue = vm[key] //老值
        Dep.target = null;  //防止多次调用
    }
    
    //当数据发生变化时更新视图
    update() {
        let newValue = this.vm[this.key]
        if(this.oldValue === newValue) return
        this.cb(newValue)
    }


}